package com.zhongke.cotrun.controller;

import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.pojo.AgentInfo;
import com.zhongke.cotrun.model.pojo.UserInfo;
import com.zhongke.cotrun.model.pojo.UserInforCompany;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.AgentInfoService;
import com.zhongke.cotrun.service.AgentJobsService;
import com.zhongke.cotrun.service.CommonConfigService;
import com.zhongke.cotrun.service.UserInfoService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

 /**
 * @Description: 经纪人附加信息
 * @Author: songjing
 * @Date:   2020-03-28
 * @Version: V1.0
 */
@Api(tags = "经纪人附加信息管理")
@RestController
@RequestMapping("/agentInfo")
@Validated
public class AgentInfoController {
	@Autowired
	private AgentInfoService agentInfoService;	
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	CommonConfigService commonConfigService;
	@Autowired
	AgentJobsService agentJobsService;
	/**
	 * 经纪人认证添加
	 * @param AgentInfo
	 * @return
	 */
	@ApiOperation(value = "认证经纪人")
	@PostMapping(value = "/add")
	public APIResponse add(@RequestBody AgentInfo agentInfo) {		
		if(ShiroUtils.getWebUserId() == null)
			return APIResponse.returnFail(CodeMsg.ACCOUNT_ERROR);	
		if(agentInfo == null || StringUtil.isEmpty(agentInfo.getResearchArea()) || StringUtil.isEmpty(agentInfo.getSkillArea())
				|| StringUtil.isEmpty(agentInfo.getImagePhoto()) || StringUtil.isEmpty(agentInfo.getAutPhotoFront())
				|| StringUtil.isEmpty(agentInfo.getAutPhotoBack()) || StringUtil.isEmpty(agentInfo.getApplyNote())
				)
			return APIResponse.returnFail(CodeMsg.BIND_ERROR);
		try {
			//验证用户
			UserInfo userInfo = userInfoService.selectUserInfoByid(ShiroUtils.getWebUserId().intValue());
			if(userInfo == null || userInfo.getState() != 0)//用户状态必须为正常
				return APIResponse.returnFail(CodeMsg.USER_TYPE_ERROR);	
			if("1".equals(userInfo.getType()) && userInfo.getComplete() != 1)//个人用户必须完善信息
				return APIResponse.returnFail(CodeMsg.USER_TYPE_ERROR);
			if("2".equals(userInfo.getType()))
			{
				UserInforCompany userInforCompany = userInfoService.selectUserInfoCompanyById(userInfo.getUserId());
				if(userInforCompany == null || userInforCompany.getAuditStatus() != 1)//机构用户的审核状态必须为通过
					return APIResponse.returnFail(CodeMsg.USER_TYPE_ERROR);
			}
			
			agentInfo.setUserInfoId(ShiroUtils.getWebUserId());
			agentInfo.setCreateBy(agentInfo.getUserInfoId());
			agentInfo.setCreateDate(new Date());
			agentInfoService.save(agentInfo);
			
			userInfoService.addUserEnditiy("经纪人", ShiroUtils.getWebUserId().intValue());//用户添加身份
			return APIResponse.returnSuccess("认证经纪人成功");
		} catch (Exception e) {
			e.printStackTrace();
			return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
		}
	}
	
	/**
	  *  编辑
	 * @param PayMallInfo
	 * @return
	 */
	@ApiOperation(value = "编辑经纪人认证信息")
	@PostMapping(value = "/update")
	public APIResponse update(@RequestBody AgentInfo agentInfo) {		
		if(ShiroUtils.getWebUserId() == null)
			return APIResponse.returnFail(CodeMsg.ACCOUNT_ERROR);	
		if(agentInfo == null || agentInfo.getId() == null || StringUtil.isEmpty(agentInfo.getResearchArea())
				|| StringUtil.isEmpty(agentInfo.getSkillArea())
				|| StringUtil.isEmpty(agentInfo.getImagePhoto()) || StringUtil.isEmpty(agentInfo.getAutPhotoFront())
				|| StringUtil.isEmpty(agentInfo.getAutPhotoBack()) || StringUtil.isEmpty(agentInfo.getApplyNote())
				)
			return APIResponse.returnFail(CodeMsg.BIND_ERROR);
		
		AgentInfo agentInfoc = agentInfoService.getById(agentInfo.getId());
		if(agentInfoc==null || !agentInfoc.getUserInfoId().equals(ShiroUtils.getWebUserId()))
			return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
		
		agentInfo.setUserInfoId(ShiroUtils.getWebUserId());
		agentInfo.setUpdateBy(agentInfo.getUserInfoId());
		agentInfo.setUpdateDate(new Date());
		if(agentInfoService.updateById(agentInfo))
			return APIResponse.returnSuccess("编辑成功");
		else
			return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
	}
	/**
	 * 推荐经纪人
	 * @param num 需要推荐数量
	 * @param skillArea技术领域
	 * @param researchArea 关注行业
	 * @return
	 */
	@ApiOperation(value = "推荐经纪人")
	@GetMapping(value = "/Recommend")
	public APIResponse recommend(@RequestParam(defaultValue = "4") Integer num,String skillArea,String researchArea) {
		if(ShiroUtils.getWebUserId() == null)
			return APIResponse.returnFail(CodeMsg.ACCOUNT_ERROR);	
		List list = agentInfoService.recommendList(num,skillArea,researchArea);		
		if(list != null && list.size()>0)
		{
			for(int i=0;i<list.size();i++)
			{
				AgentInfo agentInfo = (AgentInfo)list.get(i);
				if(agentInfo != null && agentInfo.getUserInfoId() != null)
				{
					UserInfo userInfo = userInfoService.selectUserInfoByid(agentInfo.getUserInfoId().intValue());
					if(userInfo != null)
					{
						agentInfo.setCity(userInfo.getCity());
						agentInfo.setProvince(userInfo.getProvince());
						agentInfo.setUserInfoPhone(userInfo.getPhoneNumber());
						agentInfo.setUserInfoName(userInfo.getRealName());
						agentInfo.setUserInfoType(userInfo.getType());
					}
					agentInfo = agentInfoService.formatLabel(agentInfo);
				}
			}
		}
		return APIResponse.returnSuccess(list);
	}
	
	
	/**
	  * 分页列表查询
	 * @param skillArea技术领域
	 * @param researchArea 关注行业
	 * @param province省
	 * @param city城市
	 * @param pageNum页码
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "前端官网查看经纪人列表")
   @GetMapping(value= {"/selectAll"},produces = "application/json;charset=utf-8")
	public APIResponse selectAll(String skillArea,String researchArea,String province,String city,
			@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10")Integer pageSize) {
		PageInfo page = agentInfoService.selectAll("1",skillArea,researchArea, province,city, pageNum, pageSize);
		List list = page.getList();
		if(list != null && list.size()>0)
		{
			for(int i=0;i<list.size();i++)
			{
				AgentInfo agentInfo = (AgentInfo)list.get(i);
				agentInfo = agentInfoService.formatLabel(agentInfo);
			}
		}
		return APIResponse.returnSuccess(page);
	}
	/**
	 * 通过id查询
	 * @param id经纪人主键
	 * @param userInfoId用户主键
	 * @param phone用户手机号
	 * @return
	 */
	@ApiOperation(value = "查看指定经纪人详情")
	@GetMapping(value = "/selectDetail")
	public APIResponse selectDetail(Long id,Long userInfoId,String phone) {
		if(id == null && userInfoId== null && StringUtil.isEmpty(phone))
			return APIResponse.returnFail(CodeMsg.BIND_ERROR);
		
		AgentInfo agentInfo = agentInfoService.selectDetailByUser(id,userInfoId, phone);
		if(agentInfo==null)
			return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
		else			
			return APIResponse.returnSuccess(agentInfoService.formatLabel(agentInfo));
	}
}
